angular.module('inspinia', ['uiSwitch', 'infinity.angular-chosen']).controller('workProductConfig', function ($scope, $http, SweetAlert, $state, $stateParams, i18nService, $timeout) {
  //数据源
  i18nService.setCurrentLang('zh-cn');

  $scope.nameSelect = [];
  $scope.nameSelect1 = [];
  $scope.nameSelect2 = [];
  $scope.workTypeList = [];
  $scope.countTypeList = [];
  $scope.warnInfo = {};
  $scope.info = {};
  $scope.disabledFlag = true;

  $scope.toUpdate = function(){
    $scope.disabledFlag = false;
  }

  //获取工单配置
  $scope.getWorkProductConfig = function () {
    $http.get('workProductConfig/info')
      .success(function (result) {
        if (!result.status) {
          $scope.notice(result.msg)
        }
        if (result.data != null) {
          $scope.initInfo(result.data)
        }
      });

  }
  $scope.getWorkProductConfig();

  $scope.initInfo = function (data) {
    $scope.info = data;
    if ($scope.info.warningConfig != null) {
      $scope.warnInfo = JSON.parse($scope.info.warningConfig);
    }
    if ($scope.info.workTypes != null && $scope.info.workTypes.length > 0) {
      $scope.workTypeList = angular.copy($scope.info.workTypes);
    }
    if ($scope.info.countTypes != null && $scope.info.countTypes.length > 0) {
      $scope.countTypeList = angular.copy($scope.info.countTypes);
    }
  }

  //获取所有工单类型
  $scope.getAllWorkType = function () {
    $http.get('workType/getAll')
      .success(function (result) {
        if (!result.status) {
          return;
        }
        if (result.data == null || result.data.length == 0) {
          $scope.nameSelect.push({value: "", text: "全部"});
        } else {
          $scope.nameSelect.push({value: "", text: "全部"});
          for (var i = 0; i < result.data.length; i++) {
            $scope.nameSelect.push({value: result.data[i].id, text: result.data[i].name});
          }
        }
        $scope.nameSelect1 = angular.copy($scope.nameSelect);
        $scope.nameSelect2 = angular.copy($scope.nameSelect);
      });
  }
  $scope.getAllWorkType();

  //type 1 : 首刷投诉字段配置 2: 数据统计范围配置
  $scope.workTypeChange = function (typeId) {
    if (typeId == 1) {
      var existFlag = false;
      $scope.nameSelect1.forEach(item => {
        if (item.value == $scope.info.workTypeId) {
          //判断是否有重复记录 有就跳过
          $scope.workTypeList.forEach(entity => {
            if (entity.id == $scope.info.workTypeId) {
              existFlag = true;
            }
          })

          if (!existFlag) {
            $scope.workTypeList.push({id: $scope.info.workTypeId, name: item.text});
          }
        }
      });

      $scope.workTypeListGrid.isRowSelectable();
    } else if (typeId == 2) {
      var existFlag = false;
      $scope.nameSelect2.forEach(item => {
        if (item.value == $scope.info.countWorkTypeId) {
          //判断是否有重复记录 有就跳过
          $scope.countTypeList.forEach(entity => {
            if (entity.id == $scope.info.countWorkTypeId) {
              existFlag = true;
            }
          })
          if (!existFlag) {
            $scope.countTypeList.push({id: $scope.info.countWorkTypeId, name: item.text});
          }
        }
      });
      $scope.countTypeListGrid.isRowSelectable();
    }
  }

  $scope.configSwitchUpdate = function () {

  }

  $scope.workTypeListGrid = {
    data: 'workTypeList',
    enableVerticalScrollbar: true,  		//纵向滚动条
    columnDefs: [
      {field: 'name', displayName: '工单类型', width: 300}
    ],
    onRegisterApi: function (gridApi) {
      $scope.gridApiProduct = gridApi;
      //全选
      $scope.gridApiProduct.selection.on.rowSelectionChangedBatch($scope, function (rows) {
        if (rows[0].isSelected) {
          $scope.workTypeList = [];
          var existed = false;
          $scope.workTypeList.forEach(entity=>{
            if(entity.id = row.entity.id){
              existed = true;
            }
          })
          if(existed){
            return;
          }
          rows.forEach(row => {
            $scope.workTypeList.push({id: row.entity.id, name: row.entity.name});
          })
        }else{
          for (var i in $scope.workTypeList) {
            if ($scope.workTypeList[i].id == row.entity.id) {
              row.isSelected = false;
              $scope.workTypeList[i].delFlag = true;
            }
          }
        }
      })
      $scope.gridApiProduct.selection.on.rowSelectionChanged($scope, function (row) {
        if (row.isSelected) {
          var existed = false;
          $scope.workTypeList.forEach(entity=>{
            if(entity.id = row.entity.id){
              existed = true;
            }
          })
          if(existed){
            return;
          }
          $scope.workTypeList.push({id: row.entity.id, name: row.entity.name});
        } else {
          for (var i in $scope.workTypeList) {
            if ($scope.workTypeList[i].id == row.entity.id) {
              row.isSelected = false;
              $scope.workTypeList[i].delFlag = true;
            }
          }
        }
      })
    },
    isRowSelectable: function (row) {
      if (row) {
        row.isSelected = true;
      }
    }
  };

  $scope.countTypeListGrid = {
    data: 'countTypeList',
    enableVerticalScrollbar: true,  		//纵向滚动条
    columnDefs: [
      {field: 'name', displayName: '工单类型', width: 300}

    ],
    onRegisterApi: function (gridApi) {
      $scope.gridApiProduct = gridApi;
      //全选
      $scope.gridApiProduct.selection.on.rowSelectionChangedBatch($scope, function (rows) {
        $scope.countTypeList = [];
        if (rows[0].isSelected) {
          rows.forEach(row => {
            $scope.countTypeList.push({id: row.entity.id, name: row.entity.name});
          })
        }
      })
      $scope.gridApiProduct.selection.on.rowSelectionChanged($scope, function (row) {
        if (row.isSelected) {
          var existed = false;
          $scope.countTypeList.forEach(entity=>{
            if(entity.id = row.entity.id){
              existed = true;
            }
          })
          if(existed){
            return;
          }
          $scope.countTypeList.push({id: row.entity.id, name: row.entity.name});
        } else {
          for (var i in $scope.countTypeList) {
            if ($scope.countTypeList[i].id == row.entity.id) {
              row.isSelected = false;
              $scope.countTypeList[i].delFlag = true;
            }
          }
        }
      })
    },
    isRowSelectable: function (row) {
      if (row) {
        row.isSelected = true;
      }
    }
  };

  $scope.submit = function () {
    if($scope.warnInfo.agent.warningNumber && $scope.warnInfo.agent.warningNumber<=0){
      $scope.notice("直属代理商投诉笔数请输入正整数");
      return;
    }

    if(!(/(^[1-9]\d*$)/.test($scope.warnInfo.agent.warningNumber))) {
      $scope.notice("直属代理商投诉笔数请输入正整数");
      return;
    }
    if($scope.warnInfo.oneLevelAgent.warningNumber && $scope.warnInfo.oneLevelAgent.warningNumber<=0){
      $scope.notice("一级代理商投诉笔数请输入正整数");
      return;
    }
    if(!(/(^[1-9]\d*$)/.test($scope.warnInfo.oneLevelAgent.warningNumber))) {
      $scope.notice("一级代理商投诉笔数请输入正整数");
      return;
    }

    $scope.info.workTypeList = "";
    $scope.info.countTypeList = "";

    if ($scope.workTypeList != null && $scope.workTypeList.length > 0) {
      $scope.workTypeList.forEach(entity => {
        if (!entity.delFlag) {
          $scope.info.workTypeList += entity.id + ",";
        }

      })
      $scope.info.workTypeList = $scope.info.workTypeList.substr(0, $scope.info.workTypeList.length - 1)
    }

    if ($scope.countTypeList != null && $scope.countTypeList.length) {
      $scope.countTypeList.forEach(entity => {
        if (!entity.delFlag) {
          $scope.info.countTypeList += entity.id + ",";
        }
      })
      $scope.info.countTypeList = $scope.info.countTypeList.substr(0, $scope.info.countTypeList.length - 1)
    }

    $scope.info.warningSwitch = $scope.info.warningSwitch ? 1 : 0;
    if (JSON.stringify($scope.warnInfo) != '{}') {
      $scope.info.warningConfig = angular.toJson($scope.warnInfo);
    }
    $http({
      url: 'workProductConfig/update',
      method: 'POST',
      data: $scope.info
    }).success(function (result) {
      $scope.submitting = false;
      $scope.notice(result.msg);
    });
  }

});


